//
//  MyWorkInfoVC.m
//  JinFuThree
//
//  Created by 毛韶谦 on 2018/9/3.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "MyWorkInfoVC.h"
#import "ImportTextView.h"
#import "ImportSelectView.h"
#import "MyInfoModel.h"
#import "MyRequestTools.h"

@interface MyWorkInfoVC ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UILabel *headerLabel;
@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) ImportTextView *nameTextView;
@property (nonatomic, strong) ImportTextView *addressTextView;
@property (nonatomic, strong) ImportTextView *phoneTextView;
@property (nonatomic, strong) ImportTextView *positionTextView;

@property (nonatomic, strong) ImportSelectView *incomeTypeView;
@property (nonatomic, strong) ImportSelectView *incomeMonthView;

@property (nonatomic, strong) UIButton *tiJiaoBtn;
@property (nonatomic, strong) UIPickerView *pickerView;

@property (nonatomic, strong) MyInfoCompanyModel *companyModel;

@property (nonatomic, strong) NSArray *dataArr;

@end

@implementation MyWorkInfoVC

{
    NSInteger selectSourceRow;
    NSInteger selectMonthlyRow;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavigationBar];
    [self initUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self updateData];
}

- (void)initNavigationBar {
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setImage:[[UIImage imageNamed:@"BackArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 2.5, 30, 41.5f);
    [button1 addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setImage:[[UIImage imageNamed:@"nav_close"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button2.frame = CGRectMake(30, 2.5, 30, 41.5f);
    [button2 addTarget:self action:@selector(backRootAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button2];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backgroundView];
}
- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)backRootAction {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)initUI {
    
    self.title = @"单位信息";
    self.mainScrollView.backgroundColor = ListMaoBackgroundColor;
    
    [self.nameTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerLabel.mas_left).offset(-20);
        make.right.mas_equalTo(self.headerLabel.mas_right).offset(20);
        make.top.mas_equalTo(self.headerLabel.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    [self.addressTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.nameTextView);
        make.top.mas_equalTo(self.nameTextView.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    [self.phoneTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.nameTextView);
        make.top.mas_equalTo(self.addressTextView.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    [self.positionTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.nameTextView);
        make.top.mas_equalTo(self.phoneTextView.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    [self.incomeTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.nameTextView);
        make.top.mas_equalTo(self.positionTextView.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    [self.incomeMonthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.nameTextView);
        make.top.mas_equalTo(self.incomeTypeView.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    
    [self.tiJiaoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.headerLabel);
        make.top.mas_equalTo(self.incomeMonthView.mas_bottom).offset(30);
        make.height.mas_equalTo(45);
    }];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)HexColor(0xD6C096).CGColor, (__bridge id)HexColor(0xBEA471).CGColor];
    gradientLayer.locations = @[@0, @1];
    gradientLayer.startPoint = CGPointMake(0, 1);
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.frame = CGRectMake(0, 0, ScreenWidthMao - 40, 45);
    gradientLayer.cornerRadius = 4;
    gradientLayer.masksToBounds = YES;
    [self.tiJiaoBtn.layer addSublayer:gradientLayer];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor whiteColor];
    label.text = @"提 交";
    [_tiJiaoBtn addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.tiJiaoBtn);
    }];
}

- (void)updateData {
    
    [HFQIndicationView showInView:self.view];
    [MyRequestTools MyInfoGetCompanyWithParameters:nil Success:^(id  _Nullable responseObject) {
        [HFQIndicationView dismiss];
        NSDictionary *dataDic = responseObject[@"data"];
        [self.companyModel setValuesForKeysWithDictionary:dataDic];
        self.nameTextView.leftTextView.text = self.companyModel.companyName;
        self.addressTextView.leftTextView.text = self.companyModel.companyAddress;
        self.phoneTextView.leftTextView.text = self.companyModel.companyPhone;
        self.positionTextView.leftTextView.text = self.companyModel.position;
        
        self.incomeTypeView.rightLabel.accessibilityLabel = self.companyModel.sourceIncome;
        self.incomeMonthView.rightLabel.accessibilityLabel = self.companyModel.monthlyIncome;
        self.incomeTypeView.rightLabel.text = self.companyModel.sourceIncomeVal;
        self.incomeMonthView.rightLabel.text = self.companyModel.monthlyIncomeVal;
        self.incomeTypeView.rightLabel.textColor = HexColor(0x333333);
        self.incomeMonthView.rightLabel.textColor = HexColor(0x333333);
        
        [self cheakAllData];
    } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        [HFQIndicationView dismiss];
        
    }];
}



- (void)textViewChange:(UITextField *)textField {
    
    [self cheakAllData];
}

- (void)clickAction:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    NSString *key = @"Source_of_income";
    if (sender.tag == 2) {
        key = @"MONTHLY_INCOME";
    }
    
    [HFQIndicationView showInView:self.view];
    [MyRequestTools getDicDataWithdicKey:key Success:^(id  _Nullable responseObject) {
        [HFQIndicationView dismiss];
        
            self.dataArr = [NSArray arrayWithArray:[responseObject objectForKey:@"data"]];
        if (self.dataArr.count) {
            
            selectMonthlyRow = 0;
            selectSourceRow = 0;
            [self showPickerView:sender.tag];
        }
        
    } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        [HFQIndicationView dismiss];
        
    }];
}

- (void)tiJiaoBtnAction {
    
    if ([self cheakAllData]) {
        
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
        
        [tempDic setObject:self.nameTextView.leftTextView.text forKey:@"companyName"];
        [tempDic setObject:self.addressTextView.leftTextView.text forKey:@"companyAddress"];
        [tempDic setObject:self.phoneTextView.leftTextView.text forKey:@"companyPhone"];
        [tempDic setObject:self.positionTextView.leftTextView.text forKey:@"position"];
        [tempDic setObject:self.incomeTypeView.rightLabel.accessibilityLabel forKey:@"sourceIncome"];
        [tempDic setObject:self.incomeMonthView.rightLabel.accessibilityLabel forKey:@"monthlyIncome"];
        
        [HFQIndicationView showInView:self.view];
        [MyRequestTools MyInfoCompanyWithParameters:tempDic Success:^(id  _Nullable responseObject) {
            [HFQIndicationView dismiss];
            
            [self backAction];
        } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
            [HFQIndicationView dismiss];
            
        }];
    }
}

//检查提交按钮是否显示
-(BOOL)cheakAllData {
    
    self.tiJiaoBtn.alpha = 0.5;
    self.tiJiaoBtn.userInteractionEnabled = NO;
    if (self.nameTextView.leftTextView.text.length == 0) {
        return NO;
    }else
    if (self.addressTextView.leftTextView.text.length == 0) {
        return NO;
    }else
    if (self.phoneTextView.leftTextView.text.length == 0) {
        return NO;
    }else
    if (self.positionTextView.leftTextView.text.length == 0) {
        return NO;
    }else
    if (self.incomeTypeView.rightLabel.accessibilityLabel.length == 0) {
        return NO;
    }else
    if (self.incomeMonthView.rightLabel.accessibilityLabel.length == 0) {
        return NO;
    }
    self.tiJiaoBtn.userInteractionEnabled = YES;
    self.tiJiaoBtn.alpha = 1;
    return YES;
}

//弹窗
-(void)showPickerView:(NSInteger)tag {
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight)];
    backgroundView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4/1.0];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancleAction:)];
    [backgroundView addGestureRecognizer:tap];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 240, ScreenHeight, 240)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:whiteView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(80, 0, ScreenWidthMao - 80 - 50, 50);
    [whiteView addSubview:leftBtn];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.frame = CGRectMake(0, 0, 80, 50);
    [leftLabel setTextAlignment:NSTextAlignmentCenter];
    leftLabel.text = @"取消";
    leftLabel.textColor = HexColor(0x454444);
    leftLabel.font = [UIFont boldSystemFontOfSize:16];
    [whiteView addSubview:leftLabel];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(ScreenWidthMao - 80, 0, 80, 50);
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [rightBtn setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [rightBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.tag = tag;
    [whiteView addSubview:rightBtn];
    
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 30, ScreenWidthMao, 190)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.tag = tag;
    [whiteView addSubview:_pickerView];
    
    [UIView animateWithDuration:0.3 animations:^{
        [[HFQCommonTools lastWindow] addSubview:backgroundView];
    }];
    
}

- (void)sureBtnAction:(UIButton *)sender{
    
    
    if (self.dataArr.count) {
        if (sender.tag == 1) {
            self.incomeTypeView.rightLabel.text = self.dataArr[selectSourceRow][@"value"];
            self.incomeTypeView.rightLabel.accessibilityLabel = self.dataArr[selectSourceRow][@"key"];
            self.incomeTypeView.rightLabel.textColor = HexColor(0x333333);
            
        }else if (sender.tag == 2) {
            self.incomeMonthView.rightLabel.text = self.dataArr[selectMonthlyRow][@"value"];
            self.incomeMonthView.rightLabel.accessibilityLabel = self.dataArr[selectMonthlyRow][@"key"];
            self.incomeMonthView.rightLabel.textColor = HexColor(0x333333);
        }
    }
    [self cheakAllData];
    UIView *whiteView = sender.superview;
    UIView *backgroundView = whiteView.superview;
    
    [UIView animateWithDuration:0.3 animations:^{
        backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        backgroundView.hidden = YES;
        [backgroundView removeFromSuperview];
    }];
}

- (void)cancleAction:(UITapGestureRecognizer *)gesture {
    
    UIView *view = gesture.view;
    [UIView animateWithDuration:0.3 animations:^{
        
        view.alpha = 0;
    } completion:^(BOOL finished) {
        view.hidden = YES;
        [view removeFromSuperview];
    }];
    [self cheakAllData];
}
#pragma mark --------------Picker 代理方法---------------

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return self.dataArr.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    
    NSDictionary *dic = self.dataArr[row];
    return dic[@"value"];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    switch (pickerView.tag) {
        case 1:
        {
            
            selectSourceRow = row;
            
        }
            break;
        case 2:
        {
            selectMonthlyRow = row;
            
        }
            break;
            
        default:
            break;
    }
}


- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight - SafeAreaTopHeight)];
        _mainScrollView.contentSize = CGSizeMake(ScreenWidthMao, 450);
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.bounces = NO;
        
        [self.view addSubview:_mainScrollView];
    }
    return _mainScrollView;
}

- (UILabel *)headerLabel {
    
    if (!_headerLabel) {
        _headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, ScreenWidthMao - 40, 40)];
        _headerLabel.text = @"请确保你本人的信息真实有效";
        _headerLabel.numberOfLines = 2;
        _headerLabel.font = [UIFont systemFontOfSize:12];
        _headerLabel.textColor = HexColor(0x999999);
        [_headerLabel setTextAlignment:NSTextAlignmentLeft];
        _headerLabel.backgroundColor = HexColor(0xF4F4F4);
        
        [self.mainScrollView addSubview:_headerLabel];
    }
    return _headerLabel;
}

- (ImportTextView *)nameTextView {
    
    if (!_nameTextView) {
        _nameTextView = [[ImportTextView alloc] init];
        _nameTextView.titleLabel.text = @"单位名称";
        [_nameTextView.leftTextView addTarget:self action:@selector(textViewChange:) forControlEvents:UIControlEventEditingChanged];
        
        [self.mainScrollView addSubview:_nameTextView];
        [_nameTextView.leftTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_nameTextView.mas_right).offset(-30);
        }];
    }
    return _nameTextView;
}
- (ImportTextView *)addressTextView {
    
    if (!_addressTextView) {
        _addressTextView = [[ImportTextView alloc] init];
        _addressTextView.titleLabel.text = @"单位地址";
        [_addressTextView.leftTextView addTarget:self action:@selector(textViewChange:) forControlEvents:UIControlEventEditingChanged];
        
        [self.mainScrollView addSubview:_addressTextView];
        [_addressTextView.leftTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_addressTextView.mas_right).offset(-30);
        }];
    }
    return _addressTextView;
}
- (ImportTextView *)phoneTextView {
    
    if (!_phoneTextView) {
        _phoneTextView = [[ImportTextView alloc] init];
        _phoneTextView.titleLabel.text = @"单位电话";
        [_phoneTextView.leftTextView setKeyboardType:UIKeyboardTypePhonePad];
        [_phoneTextView.leftTextView addTarget:self action:@selector(textViewChange:) forControlEvents:UIControlEventEditingChanged];
        
        [self.mainScrollView addSubview:_phoneTextView];
        [_phoneTextView.leftTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_phoneTextView.mas_right).offset(-30);
        }];
    }
    return _phoneTextView;
}
- (ImportTextView *)positionTextView {
    
    if (!_positionTextView) {
        _positionTextView = [[ImportTextView alloc] init];
        _positionTextView.titleLabel.text = @"担任职位";
        [_positionTextView.leftTextView addTarget:self action:@selector(textViewChange:) forControlEvents:UIControlEventEditingChanged];
        
        [self.mainScrollView addSubview:_positionTextView];
        [_positionTextView.leftTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_positionTextView.mas_right).offset(-30);
        }];
    }
    return _positionTextView;
}
- (ImportSelectView *)incomeTypeView {
    
    if (!_incomeTypeView) {
        _incomeTypeView = [[ImportSelectView alloc] init];
        _incomeTypeView.titleLabel.text = @"收入来源";
        
        [self.mainScrollView addSubview:_incomeTypeView];
        
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.tag = 1;
        [leftBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_incomeTypeView addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(_incomeTypeView);
            make.left.mas_equalTo(_incomeTypeView.titleLabel.mas_right);
        }];
    }
    return _incomeTypeView;
}
- (ImportSelectView *)incomeMonthView {
    
    if (!_incomeMonthView) {
        _incomeMonthView = [[ImportSelectView alloc] init];
        _incomeMonthView.titleLabel.text = @"每月收入";
        _incomeMonthView.lineImageView.hidden = YES;
        
        [self.mainScrollView addSubview:_incomeMonthView];
        
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.tag = 2;
        [leftBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_incomeMonthView addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(_incomeMonthView);
            make.left.mas_equalTo(_incomeMonthView.titleLabel.mas_right);
        }];
    }
    return _incomeMonthView;
}

- (UIButton *)tiJiaoBtn {
    
    if (!_tiJiaoBtn) {
        _tiJiaoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tiJiaoBtn addTarget:self action:@selector(tiJiaoBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _tiJiaoBtn.alpha = 0.5;
        _tiJiaoBtn.userInteractionEnabled = NO;
        
        [self.view addSubview:_tiJiaoBtn];
    }
    return _tiJiaoBtn;
}

- (MyInfoCompanyModel *)companyModel {
    
    if (!_companyModel) {
        _companyModel = [[MyInfoCompanyModel alloc] init];
    }
    return _companyModel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
